package com.zengde.ai_sandbox.controller;

import com.zengde.ai_sandbox.service.SemanticChunkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/semantic")
public class SemanticChunkController {

    @Autowired
    private SemanticChunkService semanticChunkService;

    /**
     * 对文本进行语义分块
     */
    @PostMapping("/chunk")
    public ResponseEntity<?> chunkText(
            @RequestBody String text,
            @RequestParam(defaultValue = "50") int chunkSize,
            @RequestParam(defaultValue = "10") int overlap) {
        
        try {
            List<String> chunks = semanticChunkService.chunkTextBySemantic(text, chunkSize, overlap);
            
            Map<String, Object> response = new HashMap<>();
            response.put("original_text", text);
            response.put("chunk_size", chunkSize);
            response.put("overlap", overlap);
            response.put("chunks", chunks);
            response.put("chunk_count", chunks.size());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of(
                "error", e.getMessage()
            ));
        }
    }
    
    /**
     * 使用示例商品详情进行语义分块演示
     */
    @GetMapping("/demo")
    public ResponseEntity<?> demoChunk(
            @RequestParam(defaultValue = "50") int chunkSize,
            @RequestParam(defaultValue = "10") int overlap) {
        
        // 示例商品详情文本
        String productDetails = "品牌禾果小镇城市定西市上市时间11月 10月 9月售卖方式产地直销重量2500g 4500g 3斤 1斤配送频次1周1次套餐份量5人份产地中国大陆省份甘肃省套餐周期1周包装方式包装特产品类定西马铃薯单果规格中果 大果 小果食用方式蒸煮食用厂名禾果小镇厂址甘肃省定西市临洮县厂家联系方式15309331947保质期7天";
        
        try {
            List<String> chunks = semanticChunkService.chunkTextBySemantic(productDetails, chunkSize, overlap);
            
            Map<String, Object> response = new HashMap<>();
            response.put("original_text", productDetails);
            response.put("chunk_size", chunkSize);
            response.put("overlap", overlap);
            response.put("chunks", chunks);
            response.put("chunk_count", chunks.size());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of(
                "error", e.getMessage()
            ));
        }
    }
} 